import { isUndef } from "../../help";

/*
普通dom属性, 如alt, src, id什么的
*/
function updateAttrs(oldVnode, vnode){
    // attrs: 静态属性, props: 动态属性
    const data = vnode.data || {};
    const olddata = oldVnode.data || {};

    let domAttrs = {};
    Object.assign(domAttrs, data.attrs || {});
    Object.assign(domAttrs, data.props || {});

    let oldDomAttrs = olddata.oldDomAttrs || {};
    let propName;
    const elm = vnode.elm;
    // 老的有，新的没了，应该移除
    for(propName in oldDomAttrs){
        if (isUndef(domAttrs[propName])){
            elm.removeAttribute(propName);
        }
    }
    for (propName in domAttrs){
        if (domAttrs[propName] !== oldDomAttrs[propName]){
            setAttribute(elm, propName, domAttrs[propName]);
        }
    }
}
function setAttribute(el, key, value){
    // 暂时先用 setAttribute去设置(会有bug, 比如checkbox就会出问题)
    el.setAttribute(key,value);
}
export default{
    create: updateAttrs,
    update: updateAttrs
}